package com.meeting.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meeting.annotation.AuthCheck;
import com.meeting.common.result.Result;
import com.meeting.model.dto.bookingorder.BookingOrderCreateRequest;
import com.meeting.model.dto.bookingorder.BookingOrderPayRequest;
import com.meeting.model.dto.bookingorder.BookingRequirementRequest;
import com.meeting.model.dto.customer.CustomerLoginRequest;
import com.meeting.model.dto.customer.CustomerRegisterRequest;
import com.meeting.model.vo.BookingOrderVO;
import com.meeting.model.vo.MeetingRoomRecommendVO;
import com.meeting.service.BookingOrderService;
import com.meeting.service.CustomerService;
import com.meeting.service.MeetingRoomService;
import com.meeting.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/customer")
@Tag(name = "客户接口")
public class CustomerController {

    @Resource
    private CustomerService customerService;
    
    @Resource
    private BookingOrderService bookingOrderService;

    @Resource
    private MeetingRoomService meetingRoomService;

    @PostMapping("/register")
    @Operation(summary = "客户注册", description = "客户通过填写信息进行注册")
    public Result<Boolean> register(@Valid @RequestBody CustomerRegisterRequest registerRequest) {
        Boolean result = customerService.register(registerRequest);
        return Result.success(result);
    }

    @PostMapping("/login")
    @Operation(summary = "客户登录", description = "客户通过账号密码进行登录认证")
    public Result<String> login(@Valid @RequestBody CustomerLoginRequest loginRequest) {
        String token = customerService.login(loginRequest.getUsername(), loginRequest.getPassword());
        return Result.success(token);
    }
    

    @PostMapping("/recommend-meeting-rooms")
    @Operation(summary = "推荐会议室", description = "根据客户需求推荐合适的会议室")
    @AuthCheck
    public Result<Page<MeetingRoomRecommendVO>> recommendMeetingRooms(
            @Valid @RequestBody BookingRequirementRequest requirementRequest,
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize) {
        Page<MeetingRoomRecommendVO> page = meetingRoomService.recommendMeetingRooms(requirementRequest, pageNum, pageSize);
        return Result.success(page);
    }
    
    @GetMapping("/meeting-room/{id}")
    @Operation(summary = "获取会议室详情", description = "获取会议室详细信息")
    @AuthCheck
    public Result<MeetingRoomRecommendVO> getMeetingRoomDetail(@PathVariable("id") Long meetingRoomId) {
        MeetingRoomRecommendVO vo = meetingRoomService.getMeetingRoomDetail(meetingRoomId);
        return Result.success(vo);
    }
    
    @PostMapping("/create-order")
    @Operation(summary = "创建订单", description = "客户选定会议室后创建订单")
    @AuthCheck
    public Result<BookingOrderVO> createOrder(@Valid @RequestBody BookingOrderCreateRequest createRequest,HttpServletRequest request) throws ParseException {
        String token = request.getHeader("Authorization");
        Long customerId = JwtUtil.getUserIdFromToken(token);
        BookingOrderVO orderVO = bookingOrderService.createOrder(createRequest,customerId);
        return Result.success(orderVO);
    }
    
    @PostMapping("/pay-order")
    @Operation(summary = "支付订单", description = "客户支付订单")
    @AuthCheck
    public Result<Boolean> payOrder(@Valid @RequestBody BookingOrderPayRequest payRequest, HttpServletRequest request) throws ParseException {
        Long orderId = payRequest.getOrderId();
        Integer paymentMethod = payRequest.getPaymentMethod();
        String token = request.getHeader("Authorization");
        Long customerId = JwtUtil.getUserIdFromToken(token);
        Boolean result = bookingOrderService.payOrder(orderId,customerId,paymentMethod);
        return Result.success(result);
    }
    
    @GetMapping("/order/{orderId}")
    @Operation(summary = "获取订单详情", description = "获取订单详细信息")
    @AuthCheck
    public Result<BookingOrderVO> getOrderDetail(@PathVariable("orderId") Long orderId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Long customerId = JwtUtil.getUserIdFromToken(token);
        BookingOrderVO orderVO = bookingOrderService.getOrderDetail(orderId,customerId);
        return Result.success(orderVO);
    }
    
    @GetMapping("/orders")
    @Operation(summary = "获取订单列表", description = "获取客户的订单列表")
    @AuthCheck
    public Result<Page<BookingOrderVO>> getOrderList(
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize,
            HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Long customerId = JwtUtil.getUserIdFromToken(token);
        Page<BookingOrderVO> page = bookingOrderService.getOrderList(pageNum, pageSize,customerId);
        return Result.success(page);
    }


    @GetMapping("/cancellable-orders")
    @Operation(summary = "获取可取消的订单", description = "获取客户可以取消的订单列表")
    @AuthCheck
    public Result<List<BookingOrderVO>> getCancellableOrders(HttpServletRequest request) {
        // 从当前登录用户中获取客户ID
        String token = request.getHeader("Authorization");
        Long customerId = JwtUtil.getUserIdFromToken(token);
        List<BookingOrderVO> orders = bookingOrderService.getCancellableOrders(customerId);
        return Result.success(orders);
    }

    @PostMapping("/cancel-order")
    @Operation(summary = "取消预订", description = "客户取消会议室预订")
    @AuthCheck
    public Result<BookingOrderVO> cancelOrder(@RequestParam(name = "orderId") Long orderId,HttpServletRequest request) {
        // 从当前登录用户中获取客户ID
        String token = request.getHeader("Authorization");
        Long customerId = JwtUtil.getUserIdFromToken(token);
        BookingOrderVO orderVO = bookingOrderService.cancelOrder(orderId, customerId);
        return Result.success(orderVO);
    }

    @PostMapping("/refund-order")
    @Operation(summary = "申请退款", description = "客户申请退款")
    @AuthCheck
    public Result<BookingOrderVO> refundOrder(@RequestParam(name = "orderId") Long orderId,HttpServletRequest request) {
        // 从当前登录用户中获取客户ID
        String token = request.getHeader("Authorization");
        Long customerId = JwtUtil.getUserIdFromToken(token);
        BookingOrderVO orderVO = bookingOrderService.refundOrder(orderId, customerId);
        return Result.success(orderVO);
    }
}
